package com.vj;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.vj.config.SpringConfig;
import com.vj.dao.BookDao;
import com.vj.dao.ZhujieDao;
import com.vj.service.BookService;
import com.vj.service.JiheService;
import com.vj.service.ZhujieService;
import com.vj.service.impl.BookServiceImpl;
import com.vj.service.impl.ZhujieServiceImpl;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.PathResource;
import org.springframework.core.io.Resource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

public class App {
    //使用配置类
    public static void main(String[] args) throws SQLException {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
        ZhujieDao zjDao = (ZhujieDao) ctx.getBean("zhujieDao");
        zjDao.talk();
        ZhujieService zjSrv = ctx.getBean(ZhujieServiceImpl.class);
        //注解依赖注入
        zjSrv.see();
    }

    //注解开发
    public static void mainzhujie(String[] args) throws SQLException {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        ZhujieDao zjDao = (ZhujieDao) ctx.getBean("zhujieDao");
        zjDao.talk();
        ZhujieService zjSrv = ctx.getBean(ZhujieServiceImpl.class);
        zjSrv.see();
    }

    //bean文件开发
    public static void main2(String[] args) throws SQLException {
        //原生调用
//        BookService bs = new BookServiceImpl();
//        bs.save();
        //spring方式调用
        //获取ioc容器
//        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        //获取bean
//        BookService bookDao = (BookService) ctx.getBean("bookService");
//        bookDao.save();


//        BookDao bookDao = (BookDao) ctx.getBean("bookDao");
//        bookDao.save();
//        ctx.registerShutdownHook();//设置关闭钩子 自动关闭容器
//        ctx.close();//关闭容器
//        JiheService jhs = (JiheService) ctx.getBean("jiheService");
//        jhs.say();


//        DataSource ds = (DataSource) ctx.getBean("dataSource");
//        Connection cs = ds.getConnection();
//        System.out.println(ds);
//        ComboPooledDataSource ds = (ComboPooledDataSource) ctx.getBean("ComboPooledDataSource");
//        Connection cs = ds.getConnection();
//        System.out.println(cs);

//        已弃用beanFactory获取容器
        Resource rs = new ClassPathResource("applicationContext.xml");
        BeanFactory bf = new XmlBeanFactory(rs);
        BookService bookService = bf.getBean(BookService.class);
        bookService.save();
//        创建容器
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        //FileSystemXml创建容器不推荐
//        ApplicationContext ctx2 = new FileSystemXmlApplicationContext("src\\main\\resources\\applicationContext.xml");
//        BookService bookService = ctx.getBean("bookService", BookService.class);
//        BookService bookService = (BookService) ctx.getBean("bookService");
//        BookService bookService = ctx.getBean(BookService.class);
//        获取bean
//        容器层次结构

    }
}
